const {app, BrowserWindow, Menu, shell, dialog} = require('electron')
const mainProcess = require('./main') // 这里没有用remote 哦
const template = [
  {
    label: 'File',
    submenu: [
      {
        label: 'New File',
        accelerator: 'CommandOrControl + N',
        click() {
          mainProcess.creatWindow()
        }
      },
      {
        label: 'Open File',
        accelerator: 'CommandOrControl + O',
        click(item, focusedWindow) {
          if (!focusedWindow) {
            const newWindow = mainProcess.creatWindow()
            newWindow.on('show', () => {
              mainProcess.getFileFromUser(newWindow)
            })
          }
          mainProcess.getFileFromUser(focusedWindow)
        }
      },
      {
        label: 'Save File',
        accelerator: 'CommandOrControl + S',
        click(item, focusedWindow) {
          // 没有焦点窗口时响应
          if (!focusedWindow) {
            return dialog.showErrorBox(
              `Cannot Save or Export,
               There is currently no active document to save or export
              `
            )
          }
          focusedWindow.webContents.send('save-markdown')
        }
      },
      {
        label: 'Export HTML',
        accelerator: 'Shift + CommandOrControl + S',
        // 没有焦点窗口时响应
        click(item, focusedWindow) {
          if(!focusedWindow) {
            return dialog.showErrorBox(
              `Cannot Save or Export,
               There is currently no active document to save or export
              `
            )
          }
          focusedWindow.webContents.send('save-html')
        }
      }
    ]
  },
  {
    label: 'Edit',
    submenu: [
      {
        label: 'Undo',
        accelerator: 'CommandOrControl + Z',
        role: 'undo'
      },
      {
        label: 'Redo',
        accelerator: 'Shift + CommandOrControl + Z',
        role: 'redo'
      },
      {type: 'separator'},
      {
        label: 'Cut',
        accelerator: 'CommandOrControl + X',
        role: 'cut'
      },
      {
        label: 'Copy',
        accelerator: 'CommandOrControl + C',
        role: 'copy'
      },
      {
        label: 'Paster',
        accelerator: 'CommandOrControl + V',
        role: 'paste'
      },
      {
        label: 'Select All',
        accelerator: 'CommandOrControl + A',
        role: 'selectall'
      }
    ]
  },

  {
    label: 'Window',
    submenu: [
      {
        label: 'Minimize',
        accelerator: 'CommandOrControl + M',
        role: 'minimize'
      },
      {
        label: 'Close',
        accelerator: 'CommandOrControl + W',
        role: 'close'
      }
    ]
  },
  {
    label: 'Help',
    role: 'help',
    submenu: [
      {
        label: 'Visit Website',
        click() {}
      },
      {
        label: 'Toggle Developer Tools',
        click(item, focusedWindow) {
          if (focusedWindow) {
            focusedWindow.webContents.toggleDevTools()
          }
        }
      }
    ]
  }
]
if (process.platform === 'darwin') {
  const name = 'Fire Sale'
  template.unshift({
    label: name,
    submenu: [
      {
        label: `About ${name}`,
        role: 'about'
      },
      {type: 'separator'},
      {
        label: `Services ${name}`,
        role: 'services',
        submenu: []
      },
      {type: 'separator'},
      {
        label: `Hide ${name}`,
        accelerator: 'Command + H',
        role: 'hide'
      },
      {
        label: 'Hide Others',
        accelerator: 'Command + Alt + H',
        role: 'hideothers'
      },
      {
        label: `Show All ${name}`,
        role: 'unhide'
      },
      {type: 'separator'},
      {
        label: `Quit ${name}`,
        accelerator: 'Command + Q',
        click() {app.quit()}
      }

    ]
  })
}


const windowMenu = template.find(item => item.label === 'Window')
windowMenu.role = 'window'
windowMenu.submenu.push(
  {type: 'separator'},
  {
    label: 'Bring All to Front',
    role: 'front'
  }
)
module.exports = Menu.buildFromTemplate(template)
